package face.pay.web.agency.level.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.dao.AgencyLevelDao;
import face.pay.dao.OemProfitConfigInfoDao;
import face.pay.entity.AgencyLevel;
import face.pay.entity.OemProfitConfigInfo;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.agency.level.service.IAgencyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: cjj
 * @Date: 2019/9/23
 * @Description:
 */
@Service
@Slf4j
public class AgencyServiceImpl implements IAgencyService {

    @Autowired
    AgencyLevelDao agencyLevelDao;

    @Autowired
    OemProfitConfigInfoDao  oemProfitConfigInfoDao;

    @Override
    @Transactional
    public ResultMessage editLevel(UserInfo loginUser, AgencyLevel agencyLevel) {
        log.info("编辑代理商等级");

        Integer oemId = loginUser.getOemId();

        Integer id = agencyLevel.getId();
        if(id==null){

            Integer count = this.agencyLevelDao.countByOemId(oemId);
            if(count>=3){
                return ResultMessage.createError("代理商等级数量超限");
            }else {
                agencyLevel.setOemId(oemId);
                this.agencyLevelDao.insert(agencyLevel);
            }

        }else {
            this.agencyLevelDao.update(agencyLevel);
        }

        return ResultMessage.createSuccess();
    }

    @Override
    public PageData queryPage(UserInfo loginUser,PageParam pageParam) {


        Integer oemId = loginUser.getOemId();

        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        List<AgencyLevel> oemList = this.agencyLevelDao.queryListByOemId(oemId);

        PageInfo<AgencyLevel> pageInfo = new PageInfo <AgencyLevel>(oemList);

        PageData<AgencyLevel> pageData = new PageData<AgencyLevel>(pageInfo.getTotal(),oemList);

        return pageData;
    }

    public OemProfitConfigInfo getProfitConfig(UserInfo loinUser) {

        Integer oemId = loinUser.getOemId();

        OemProfitConfigInfo oemProfitConfigInfo =  this.oemProfitConfigInfoDao.getByOemId(oemId);

        if(oemProfitConfigInfo==null){
            oemProfitConfigInfo = new OemProfitConfigInfo(oemId,3,"DEFAULT",0);
        }

        return oemProfitConfigInfo;
    }
}
